package com.thoughtworks.jsstaticanalysis;

import java.io.File;
import java.util.List;

public class FoundFunction {

    public final String funcName;
    public final int startLine;
    public final int endLine;
    public final boolean maybeConstructor;
    public final int lineCount;
    public final List<Factor> factors;

    public final String fileName;
    public double score;

    public FoundFunction(String funcName, List<Factor> factors, int startLine, int endLine, String fileName) {
        this.funcName = funcName;
        this.startLine = startLine;
        this.endLine = endLine;
        this.fileName = fileName;
        this.lineCount= 1 + endLine - startLine;
        this.factors = factors;

        maybeConstructor = (fileName.toLowerCase().equals((funcName+".js").toLowerCase()));
        this.score = calculateScorce(factors);
	}

    private double calculateScorce(List<Factor> factors) {
        double score = 0;
        for (Factor factor : factors) {
            if (factor.type.equalsIgnoreCase("for")) score += 0.9;
            else if (factor.type.equalsIgnoreCase("do")) score += 1.2;
            else if (factor.type.equalsIgnoreCase("while")) score += 1;
            else if (factor.type.equalsIgnoreCase("if")) score += 1;
            else if (factor.type.equalsIgnoreCase("assignment")) score += 0.5;
        }
        return score;
    }

    public String toString() {
		return funcName + "\t\t" + score + "\t\t" + lineCount+ "\t\t["+ startLine + ", " + endLine + "]";
	}
}

